package com.zyw.security.shiro.web.realm;

import com.zyw.security.shiro.web.authc.AuthInfo;
import com.zyw.security.shiro.web.authc.AuthService;
import com.zyw.security.shiro.web.authc.tk.WXMiniAppsAuthcToken;
import org.apache.shiro.authc.*;

/**
 * 微信小程序登录认证
 *
 * @author zhangyw
 * @date 2018/1/25 9:54
 */
public class WXMiniAppsAuthRealm extends BasicAuthRealm {

    public WXMiniAppsAuthRealm() {
        this.setName("sky_sso_wxapp_realm");
        this.setAuthenticationTokenClass(WXMiniAppsAuthcToken.class);
    }

    public WXMiniAppsAuthRealm(AuthService authService) {
        this();
        this.setAuthService(authService);
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        WXMiniAppsAuthcToken wxMiniAppsAuthcToken = (WXMiniAppsAuthcToken) token;

        AuthInfo authInfo = getAuthService().getAuthInfo(wxMiniAppsAuthcToken.getPrincipal());

        return new SimpleAuthenticationInfo(authInfo, wxMiniAppsAuthcToken.getCredentials(), getName());
    }

}
